package com.ctg.payroll2.action.sysadmin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.ComboData;
import com.ctg.payroll2.bean.filter.sysadmin.RoleFilter;
import com.ctg.payroll2.bean.po.PbSecgroup;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.service.sysadmin.RoleService;
import com.ctg.payroll2.util.Constants;
import com.ctg.payroll2.util.PayrollUtil;
import com.ctg.payroll2.util.Recursion;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.core.security.Exception.RoleNameException;
import com.ctg.platform.core.security.Exception.SecurityException;
import com.ctg.platform.core.security.entity.Resource;
import com.ctg.platform.core.security.entity.Role;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.opensymphony.xwork2.ActionSupport;

public class RoleManagerAction extends ActionSupport{
	private static final long serialVersionUID = 1481827017236051972L;
	private SpringSecurityManager springSecurityManager;
	private RoleService roleService;
	private RoleFilter filter;
	private Page result;
	private Page resultPrivilege;
	private String roleIds;
	private Role role;
	private List<String> treeListJson = new ArrayList<String>();
	private PbSecgroup root = new PbSecgroup();
	private boolean success;
	private List<Resource> privilegeString = new ArrayList<Resource>();
	/**
	 * 角色是否启用状态下拉框
	 */
	private List<ComboData> roleStautsComboData;
	private String privilegeIds;
	private boolean roleNameDuplicate;
	private String roleName;
	/**
	 * @return 返回roleName
	 */
	public String getRoleName() {
		return roleName;
	}
	/**
	 * @param roleName 设置roleName
	 */
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	/**
	 * @return 返回treeListJson
	 */
	public List<String> getTreeListJson() {
		return treeListJson;
	}
	/**
	 * @return 返回privilegeIds
	 */
	public String getPrivilegeIds() {
		return privilegeIds;
	}
	/**
	 * @param privilegeIds 设置privilegeIds
	 */
	public void setPrivilegeIds(String privilegeIds) {
		this.privilegeIds = privilegeIds;
	}
	/**
	 * @param treeListJson 设置treeListJson
	 */
	
	public void setTreeListJson(List<String> treeListJson) {
		this.treeListJson = treeListJson;
	}
	
	public String empty(){
		return SUCCESS;
	}
	/**
	 * 翻页查询方法
	 * @return
	 */
	public String query(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int start = Parse.toInteger(request.getParameter("start") , 0);
		int limit = Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE);
		
		if (filter==null) {
			filter = new RoleFilter();
		}
		filter.setEndIndex(limit);
		filter.setStartIndex(start);
		filter.setRoleName(request.getParameter("queryKey"));
		result = springSecurityManager.getRoleListByFilter(filter);
		
		return SUCCESS;
	}
	public String getRoleStatus(){
		roleStautsComboData = new ArrayList<ComboData>();
		roleStautsComboData.add(new ComboData("启用", "0"));
		roleStautsComboData.add(new ComboData("冻结", "1"));
//		roleStautsComboData = service.queryComboList(dictName, null);
//		if(roleStautsComboData.size()>0)
//			System.out.println((roleStautsComboData.get(0)).getDisplayText());
		return SUCCESS;
	}
	/**
	 * 删除角色
	 * @return
	 */
	public String deleteRoleByRolesIds(){
		SysUser sysUser= (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
		String[] array = PayrollUtil.splitStringToStringArray(roleIds);
		springSecurityManager.deleteRoleById(array, sysUser);
		return SUCCESS;
	}
	//新增角色
	public String addRole(){
		boolean b = privilegeIds==null;
		String[] array = {};
		if(!b){
			array = PayrollUtil.splitStringToStringArray(privilegeIds);
		}
		SysUser sysUser= (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
		try {
			String roleId = springSecurityManager.createSysRole(role, sysUser);
			if(!b){
				springSecurityManager.assignPrivilegeForRole(roleId, array, sysUser);
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (RoleNameException e1) {
			e1.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 复查角色
	 * @return
	 */
	public String retrieveEntity(){
		SysUser sysUser= (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
		role = springSecurityManager.getRoleByRoleId(role.getId(), sysUser);
		String s = role.getDisabled();
		if(!s.equals("")){
			s=s.equals("1")?"冻结":"启用";
			role.setDisabled(s);
		}
		List<Resource> list = springSecurityManager.getAllPrivilegesByRoleId(role.getId(), sysUser);
		List<String> strList = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			strList.add(list.get(i).getId());
		}
		privilegeIds = PayrollUtil.getJsonStringByList(strList);
		return SUCCESS;
	}
	/**
	 * 更新角色
	 * @return
	 */
	public String updateRole(){
		SysUser sysUser= (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
		boolean b = privilegeIds==null;
		String[] array = {};
		if(!b){
			array = PayrollUtil.splitStringToStringArray(privilegeIds);
		}
		try {
			springSecurityManager.updateRole(role);
			if(!b){
				springSecurityManager.assignPrivilegeForRole2(role.getId(), array, sysUser);
			}
		} catch (RoleNameException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	//展现树
	public String initializeTree(){
		List<PbSecgroup> firstNodeList = roleService.getNode(Constants.MENU_ROOT_ID);
		for (int i = 0; i < firstNodeList.size(); i++) {
			PbSecgroup p = firstNodeList.get(i);
			treeListJson.add(Recursion.getString(roleService.getTree(p.getGroupCode())));
		}
		root = roleService.getNode("0").get(0);
		success = true;
		return SUCCESS;
	}
	
	public String getResourceById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		if(id!=null){
			result = roleService.getResourcePageId(id);
		}
		
		return SUCCESS;
	}
	
	public String checkRoleNameDuplicate(){
		Role r = springSecurityManager.getRoleByRoleName(roleName, true);
		roleNameDuplicate = r!=null && !StringUtils.isEmpty(r.getId());
		return SUCCESS;
	}
/**********getter, setter方法**********/
	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	
	public void setSpringSecurityManager(SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	/**
	 * @return 返回success
	 */
	public boolean isSuccess() {
		return success;
	}
	/**
	 * @param success 设置success
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}
	/**
	 * @return 返回root
	 */
	public PbSecgroup getRoot() {
		return root;
	}
	/**
	 * @param root 设置root
	 */
	public void setRoot(PbSecgroup root) {
		this.root = root;
	}
	/**
	 * @return 返回privilegeString
	 */
	public List<Resource> getPrivilegeString() {
		return privilegeString;
	}
	/**
	 * @param privilegeString 设置privilegeString
	 */
	public void setPrivilegeString(List<Resource> privilegeString) {
		this.privilegeString = privilegeString;
	}
	/**
	 * @return 返回resultPrivilege
	 */
	public Page getResultPrivilege() {
		return resultPrivilege;
	}
	/**
	 * @param resultPrivilege 设置resultPrivilege
	 */
	public void setResultPrivilege(Page resultPrivilege) {
		this.resultPrivilege = resultPrivilege;
	}
	/**
	 * @param roleNameDuplicate 设置roleNameDuplicate
	 */
	public void setRoleNameDuplicate(boolean roleNameDuplicate) {
		this.roleNameDuplicate = roleNameDuplicate;
	}
	/**
	 * @return 返回roleNameDuplicate
	 */
	public boolean isRoleNameDuplicate() {
		return roleNameDuplicate;
	}
	/**
	 * @return 返回filter
	 */
	public RoleFilter getFilter() {
		return filter;
	}
	/**
	 * @param filter 设置filter
	 */
	public void setFilter(RoleFilter filter) {
		this.filter = filter;
	}
	/**
	 * @return 返回roleStautsComboData
	 */
	public List<ComboData> getRoleStautsComboData() {
		return roleStautsComboData;
	}
	/**
	 * @param roleStautsComboData 设置roleStautsComboData
	 */
	public void setRoleStautsComboData(List<ComboData> roleStautsComboData) {
		this.roleStautsComboData = roleStautsComboData;
	}
}